/*
	================
	requirements:
	================
		Block UI plugin : http://malsup.com/jquery/
		Jquery UI 
		jquery
	=================
	custom elements:
	=================
	data-fail-message : message to be outputed when fail
	data-success-message : message to be outputed when success happens
	data-other-message : message to be outputed when event 2 happens
	data-delete-container : container of current object to remove, incase deleting some data
	data-disable-container : disables the container when clicked, enables it after the process is finished
	url : url of data to be processed
	title : title of the command prompt that will confirm link action
*/
$(document).ready(function(){
	$('.ajax-click').on('click',function(e){
		//stop the link from doing its action
		e.preventDefault();







		var this_clicked = $(this);
		// var new_div = document.createElement("div");
		// new_div.className = 'block-ui-message';

		//get url
		var url = this_clicked.attr('url');
		var href = this_clicked.attr('href');
		var msg = this_clicked.attr('title');

		var fail_msg;
		var success_msg;

		//if fail message is not set print default fail message
		if(this_clicked.attr('data-fail-message'))
		{
			fail_msg = this_clicked.attr('data-fail-message');
		}else{
			fail_msg = 'An error has occured.';
		}

		if(this_clicked.attr('data-other-message'))
		{
			fail_msg = this_clicked.attr('data-other-message');
		}else{
			fail_msg = 'Server request unknown';
		}

		//if success message is not set print default success message
		if(this_clicked.attr('data-success-message'))
		{
			success_msg = this_clicked.attr('data-success-message');
		}else{
			success_msg = 'Action Was Successfull.';
		}

		//if an attribute url is set we will use url, else
		if(url){
			var link = url;
		}else{
			var link = href;
		}

		$( '#msgDialog > p' ).html( msg );
		$("#msgDialog").dialog('option', 'buttons', {
					"Confirm" : function() {

					if(this_clicked.attr('data-disable-container'))
					{

						var block_parent_container = $(document).find(this_clicked).parents(this_clicked.attr('data-disable-container'));
						$(block_parent_container).block({ 
                message: '<p><i>Processing...</i></p>', 
                css: { border: '3px solid #c0c0c0',
                			 padding:'0',
                			 margin:'0',
                			 '-webkit-border-radius': '10px', 
        							'-moz-border-radius':    '10px' 
                		 } 
            }); 
					}


						$(this).dialog("close");
							$.ajax({
								type:'POST',
								'url':link,
								'data':{type:'ajax'}
							}).done(function(result)
							{
								if(result == 1)
								{
									$.blockUI({ 
				                        message: success_msg, 
				                        fadeIn: 700, 
				                        fadeOut: 2000, 
				                        timeout: 4000, 
				                        showOverlay: false, 
				                        centerY: false, 
				                        css: { 
				                            width: '350px', 
				                            top: '60px', 
				                            left: '', 
				                            right: '10px', 
				                            border: '2px solid #004000', 
				                            padding: '5px', 
				                            backgroundColor: '#009500', 
				                            '-webkit-border-radius': '10px', 
				                            '-moz-border-radius': '10px', 
				                            opacity: .6, 
				                            color: '#fff' 
				                        } 
				                    });

									if(this_clicked.attr('data-delete-container'))
									{
										$(document).find(this_clicked).parents(this_clicked.attr('data-delete-container')).hide(1500);
									}

								}else if(result == 2)
								{
									$.blockUI({ 
				                        message: fail_msg, 
				                        fadeIn: 700, 
				                        fadeOut: 2000, 
				                        timeout: 4000, 
				                        showOverlay: false, 
				                        centerY: false, 
				                        css: { 
				                            width: '350px', 
				                            top: '60px', 
				                            left: '', 
				                            right: '10px', 
				                            border: 'none', 
				                            padding: '5px', 
				                            backgroundColor: '#B31717', 
				                            '-webkit-border-radius': '10px', 
				                            '-moz-border-radius': '10px', 
				                            opacity: .6, 
				                            color: '#fff' 
				                        } 
				                    });

				                    if(this_clicked.attr('data-delete-container'))
									{
										$(document).find(this_clicked).parents(this_clicked.attr('data-delete-container')).hide(1500);
									}

								}else{
									$.blockUI({ 
				                        message: fail_msg, 
				                        fadeIn: 700, 
				                        fadeOut: 2000, 
				                        timeout: 4000, 
				                        showOverlay: false, 
				                        centerY: false, 
				                        css: { 
				                            width: '350px', 
				                            top: '60px', 
				                            left: '', 
				                            right: '10px', 
				                            border: 'none', 
				                            padding: '5px', 
				                            backgroundColor: '#B31717', 
				                            '-webkit-border-radius': '10px', 
				                            '-moz-border-radius': '10px', 
				                            opacity: .6, 
				                            color: '#fff' 
				                        } 
				                    });
								}
							}).fail(function(result){
								$.blockUI({ 
	                        message: fail_msg, 
	                        fadeIn: 700, 
	                        fadeOut: 2000, 
	                        timeout: 4000, 
	                        showOverlay: false, 
	                        centerY: false, 
	                        css: { 
	                            width: '350px', 
	                            top: '60px', 
	                            left: '', 
	                            right: '10px', 
	                            border: 'none', 
	                            padding: '5px', 
	                            backgroundColor: '#000', 
	                            '-webkit-border-radius': '10px', 
	                            '-moz-border-radius': '10px', 
	                            opacity: .6, 
	                            color: '#fff' 
	                        } 
	                    }); 
							}).always(function(){

								if(this_clicked.attr('data-disable-container'))
								{
									var block_parent_container = $(document).find(this_clicked).parents(this_clicked.attr('data-disable-container'));
									$(block_parent_container).unblock(); 
								}
							});
						},
					"Cancel" : function() {
						$(this).dialog("close");
						}
			});
			$('#msgDialog').dialog('open');
	});
});